package com.yc.controller;

import com.yc.entity.UserInfo;
import com.yc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    UserService userService;
    @PostMapping("/login")
    public String login(@RequestBody UserInfo user){
       return userService.login(user);
    }

    @PostMapping("/add")
    public String add(@RequestBody UserInfo user){
        return userService.add(user);
    }

    @PostMapping("/find")
    public String find(@RequestBody UserInfo user){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String name = authentication.getName();
        System.out.println(name);
        return userService.findOne(user);
    }
    @PostMapping("/update")
    public String update(@RequestBody UserInfo user){
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        //密码加密
        String encode = bCryptPasswordEncoder.encode("123");
        user.setPassword(encode);
        return userService.update(user);
    }



    @PostMapping("/list")
    public String list( ){

        return "ok";
    }
}
